//
//  YWInformationBankTableViewController.m
//  maintain
//
//  Created by Kyo on 16/5/19.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWInformationBankTableViewController.h"
#import "UIColor+CustomColors.h"
#import "MJRefresh.h"
#import "YWMacro.h"
#import "YWConst.h"
#import "YWCommonTool.h"
#import "YWInformationBankTool.h"
#import "YWInformationBankModel.h"
#import "YWInformationBankCell.h"
#import "YWInformationDetailViewController.h"

@interface YWInformationBankTableViewController ()
@property (nonatomic, strong)NSMutableArray *sourceData;
@property (nonatomic, assign)NSInteger page;
@end

@implementation YWInformationBankTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资料库";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.rowHeight = 80;
    
    self.page = 1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    self.tableView.tableFooterView = [[UIView alloc]init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)sourceData{
    if (!_sourceData) {
        _sourceData = [NSMutableArray array];
        NSArray *arr = [[YWInformationBankTool shareManager] informationArrayWithPage:self.page];
        [_sourceData addObjectsFromArray:arr];
    }
    return _sourceData;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWInformationBankCell *cell = [YWInformationBankCell cellWithTableView:tableView];
    cell.informationModel = self.sourceData[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWInformationDetailViewController *detailVC = [[YWInformationDetailViewController alloc]init];
    YWInformationBankModel *infoModel = self.sourceData[indexPath.row];
    detailVC.attachment = infoModel.attachment;
    detailVC.title = infoModel.name;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)refreshTop
{
    self.page = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}
- (void)requestData:(YWTableViewFreshType)refreshType
{
    NSTimeInterval createTime = 0;
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
        if (self.sourceData.count) {
            YWInformationBankModel *infoModel = [self.sourceData lastObject];
            createTime = infoModel.createTime;
        }
    }
    YWWeakSelf(weakSelf);
    [[YWInformationBankTool shareManager] requestInformation:createTime WithComplete:^(BOOL success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
            else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            weakSelf.sourceData = nil;
            [weakSelf.tableView reloadData];
        });
        
    }];
}
@end
